package com.ho1ho.springboot.framework.core.utils;

import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Months;

public class DateUtils {
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd");
	public static final String DATE_SEPERATOR = "/";

	public static String getDateInStr(Integer date) {
		return getDateInStr(date, DATE_SEPERATOR);
	}

	public static String getDateInStr(Integer date, String seperator) {
		if (date == null) {
			return "";
		}
		try {
			return date / 10000 + seperator + StringUtils.leftPad(String.valueOf(date / 100 % 100), 2, "0") + seperator
					+ StringUtils.leftPad(String.valueOf(date % 100), 2, "0");
		} catch (Exception e) {
			return "";
		}
	}

	public static int getDaysToCurrentDay(String from) {
		DateTime dtFrom = new DateTime(from);
		return Days.daysBetween(new DateTime(), dtFrom).getDays();
	}

	/**
	 * Get months length between specified date and current date.
	 * 
	 * @param fromInInt
	 *            yyyyMMdd
	 * @return Get month between from to current date. If any exception occurred, 0 will be returned;
	 */
	public static int getMonthToCurrentDay(int fromInInt) {
		try {
			DateTime dtFrom = new DateTime((fromInInt / 10000) + "-" + (fromInInt / 100 % 100) + "-" + (fromInInt % 100));
			return Months.monthsBetween(dtFrom, new DateTime()).getMonths();
		} catch (Exception e) {
			return 0;
		}
	}
}
